package com.edu.robot.controller;

import cn.dev33.satoken.sso.SaSsoManager;
import cn.dev33.satoken.sso.processor.SaSsoClientProcessor;
import cn.dev33.satoken.stp.StpUtil;
import com.edu.buservice.util.model.TenantUtil;
import com.edu.robot.core.service.RobotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-02-09 15:35
 **/

@RestController
public class IndexController {

	@Autowired
	private RobotService robotService;

	// 首页
	@RequestMapping("/")
	public String index() {
		String solUrl = SaSsoManager.getClientConfig().splicingSloUrl();


		//判断是否登录
		if(StpUtil.isLogin()){
			//已登录，跳转到首页
			return "success"+ TenantUtil.getTenantContext().getTenantName();
		}else{
			//未登录，跳转到登录页面
			String str = "<h2>Sa-Token SSO-Client 应用端</h2>" +
					"<p>当前会话是否登录：" + StpUtil.isLogin() + "</p>" +
					"<p><a href=\"javascript:location.href='/sso/login?back=' + encodeURIComponent(location.href);\">登录</a> " +
					"<a href=\"javascript:location.href='" + solUrl + "?back=' + encodeURIComponent(location.href);\">注销</a> </p>";
			return str;
		}


	}

	/*
	 * SSO-Client端：处理所有SSO相关请求
	 *         http://{host}:{port}/sso/login          -- Client端登录地址，接受参数：back=登录后的跳转地址
	 *         http://{host}:{port}/sso/logout         -- Client端单点注销地址（isSlo=true时打开），接受参数：back=注销后的跳转地址
	 *         http://{host}:{port}/sso/logoutCall     -- Client端单点注销回调地址（isSlo=true时打开），此接口为框架回调，开发者无需关心
	 */
	@RequestMapping("/sso/*")
	public Object ssoRequest() {
		return SaSsoClientProcessor.instance.dister();
	}
}
